package com.may.wlrj.dao;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.may.wlrj.entity.RightEntity;
import com.may.wlrj.entity.RightRejectEntity;
import com.may.wlrj.entity.UserOperLogEntity;
import com.may.wlrj.entity.UserRightAuthEntity;
import com.may.wlrj.util.XinLongUtil;

@Repository
public class RightDAO {
	private Logger log = Logger.getLogger(RightDAO.class);
	private JdbcTemplate jdbcTemplate;

    @Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
    
    
    public int updateRight(RightEntity rightEntity){

		// update
		String sql = "UPDATE tbl_rights SET Createtime=?,Descriptor=?, Updatetime=?,Name=?,Leaf=?,Needcheck=?,Parentid=?,Status=?,Text=?, "
					+ "Create_check_username=?, Createname=?, Update_check_username=?,Updatename=?,Sort=?,Right_type=? WHERE id=?";
		try{
			jdbcTemplate.update(sql, rightEntity.getCreatetime(),rightEntity.getDescriptor(), rightEntity.getUpdatetime(),rightEntity.getName(),rightEntity.getLeaf(),rightEntity.getNeedcheck(),
					rightEntity.getParentid(),rightEntity.getStatus(),rightEntity.getText(),rightEntity.getCreate_check_username(), rightEntity.getCreatename(),rightEntity.getUpdate_check_username(),
					rightEntity.getUpdatename(),rightEntity.getSort(),rightEntity.getRight_type(),rightEntity.getId());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}
		
    	
    };
    
    
    public int saveright(RightEntity rightEntity){
    	String uId = UUID.randomUUID().toString().replace("-", "");
		// insert
		String sql = "INSERT INTO tbl_rights (id,Createtime,Descriptor, Updatetime,Name,Leaf,Needcheck,Parentid,Status,Text, Create_check_username,Createname,Update_check_username,Updatename,Sort,Right_type)"
                + " VALUES (?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(sql, uId,rightEntity.getCreatetime(),rightEntity.getDescriptor(), rightEntity.getUpdatetime(),rightEntity.getName(),rightEntity.getLeaf(),rightEntity.getNeedcheck(),rightEntity.getParentid(),
					rightEntity.getStatus(),rightEntity.getText(),rightEntity.getCreate_check_username(), rightEntity.getCreatename(),rightEntity.getUpdate_check_username(),
					rightEntity.getUpdatename(),rightEntity.getSort(),rightEntity.getRight_type());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}   	
    	
    };
    
    public List<RightEntity> findrightByPid(String pid){
    	String sql = "SELECT * FROM tbl_rights WHERE Parentid ='" + pid + "' ORDER BY Sort";  
    	try{
    		return  jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(RightEntity.class));
    	}catch(Exception e){
    		XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
    		return null;
    	}
    	
    };
    
    public RightEntity findByRightName(String name) throws EmptyResultDataAccessException{
    	String sql = "SELECT * FROM tbl_rights WHERE Name = '" + name + "'";
        try{
        	return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(RightEntity.class));
        }catch(EmptyResultDataAccessException er){
        	return null;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    	
    };
    
    public RightEntity findByRightId(String id) throws EmptyResultDataAccessException{
    	String sql = "SELECT * FROM tbl_rights WHERE id = '" + id + "'";
        try{
        	return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(RightEntity.class));
        }catch(EmptyResultDataAccessException er){
        	return null;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    	
    };
    
    public int findRightByUserid(String userid,String rightid){
    	String sql = "SELECT rg.* FROM tbl_rights rg left join tbl_role_right rr on rr.right_id = rg.id left join tbl_user_role ur on ur.role_id = rr.role_id where ur.user_id = '" + userid + "' and rg.id = '" + rightid + "'";
    	try{
    		jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(RightEntity.class));
        	return 1;
        }catch(EmptyResultDataAccessException er){
        	return 0;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return -1;
        }
    }
    
    /*================================================tbl_right_auth===============================================================*/
    public List<UserRightAuthEntity> findRightAuthAll(int pageNumber,int pageSize,String start,String end,String orgid){
    	String sql = "SELECT auth.* ,us.Username to_username,ur.Username from_username FROM tbl_right_auth auth left join tbl_users us on auth.To_userid = us.id left join tbl_users ur on auth.From_userid = ur.id  where auth.Createtime >= '" + start + "' and auth.Createtime <= '" + end + "' and us.Org_id = '" + orgid + "' ORDER BY auth.Createtime DESC LIMIT " + (pageNumber-1)*pageSize + "," + pageSize; 
    	
    	return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(UserRightAuthEntity.class));
    }
    
    public int getauthCount(String start,String end,String orgid){
    	String sql = "SELECT count(*) from tbl_right_auth auth left join tbl_users us on auth.To_userid = us.id left join tbl_users ur on auth.From_userid = ur.id where auth.Createtime >= '" + start + "' and auth.Createtime <= '" + end + "' and us.Org_id = '" + orgid + "'";
    	try{
    		return  jdbcTemplate.queryForObject(sql, Integer.class);
    	}catch(Exception e){
    		XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
    		return 0;
    	}
    }
    
    public int updateStatus(String id,String ckuserid,String uptime,String upuserid){

		// update
		String sql = "UPDATE tbl_right_auth SET Status=?,Update_check_userid=?,Updatetime=?,Updateusername=? WHERE id=?";
		try{
			jdbcTemplate.update(sql, 1,ckuserid,uptime, upuserid, id);
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}		 
    	
    }
    
    public int saveurauth(UserRightAuthEntity rightEntity){
		// insert
		String sql = "INSERT INTO tbl_right_auth (id,Createtime, Updatetime,Descriptor,Create_check_userid,Update_check_userid,Createusername,Updateusername,Start_time,End_time, Status,From_userid,To_userid)"
                + " VALUES (?, ?, ?, ?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(sql, rightEntity.getId(),rightEntity.getCreatetime(), rightEntity.getUpdatetime(),rightEntity.getDescriptor(),rightEntity.getCreate_check_userid(),rightEntity.getUpdate_check_userid(),rightEntity.getCreateusername(),rightEntity.getUpdateusername(),
					rightEntity.getStart_time(),rightEntity.getEnd_time(),rightEntity.getStatus(), rightEntity.getFrom_userid(),rightEntity.getTo_userid());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}   	
    	
    }
    
    public int updateRightAuth(UserRightAuthEntity rightEntity){

		// update
		String sql = "UPDATE tbl_right_auth SET Createtime=?,Descriptor=?, Updatetime=?,Create_check_userid=?,Update_check_userid=?,Createusername=?,Updateusername=?,Start_time=?,End_time=?, "
					+ "Status=?, From_userid=?, To_userid=? WHERE id=?";
		try{
			jdbcTemplate.update(sql, rightEntity.getCreatetime(),rightEntity.getDescriptor(), rightEntity.getUpdatetime(),rightEntity.getCreate_check_userid(),rightEntity.getUpdate_check_userid(),rightEntity.getCreateusername(),
					rightEntity.getUpdateusername(),rightEntity.getStart_time(),rightEntity.getEnd_time(),rightEntity.getStatus(), rightEntity.getFrom_userid(),rightEntity.getTo_userid(),rightEntity.getId());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}
		
    	
    };
    
    public UserRightAuthEntity findRightAuthByid(String id){
    	String sql = "SELECT * FROM tbl_right_auth where id = '" + id + "'";
    	try{    		
        	return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(UserRightAuthEntity.class));
        }catch(EmptyResultDataAccessException er){
        	return null;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    }
    
    public List<UserRightAuthEntity> findRightAuthByTo_userid(String id){
    	String sql = "SELECT * FROM tbl_right_auth where To_userid = '" + id + "' and Status = 0";
    	try{    		
        	return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(UserRightAuthEntity.class));
        }catch(EmptyResultDataAccessException er){
        	return null;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    }
    
    public int updateStatusOnly(int status,String id){

		// update
		String sql = "UPDATE tbl_right_auth SET Status=? WHERE id=?";
		try{
			jdbcTemplate.update(sql, status, id);
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}		 
    	
    }
    /*=========================================tbl_right_reject==========================================================*/
    public List<RightRejectEntity> findRejectByid(String rightid){
    	String sql = "SELECT rej.*, rg.Name rejectname,rg.Right_type rtype FROM tbl_right_reject rej left join tbl_rights rg on rej.Right_id = rg.id WHERE rej.Right_id = '" + rightid + "'";
    	try{
    		return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(RightRejectEntity.class));

        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    }
    
    public List<RightRejectEntity> findRejectdescByid(String rightid){
    	String sql = "SELECT rej.*, rg.Name rejectname,rg.Right_type rtype FROM tbl_right_reject rej left join tbl_rights rg on rej.Reject_right_id = rg.id WHERE rej.Right_id = '" + rightid + "'";
    	try{
    		return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(RightRejectEntity.class));

        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    }
    
    public int deletRejectByid(String rightid){
    	String sql = "DELETE FROM tbl_right_reject WHERE Right_id=?";
        try{
        	jdbcTemplate.update(sql, rightid);
        	return 1;
        }catch(EmptyResultDataAccessException er){
        	return 1;
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return 0;
        }
    }
    
    public int savereject(RightRejectEntity rejectEntity){
		// insert
		String sql = "INSERT INTO tbl_right_reject (id,Createtime,Descriptor, Updatetime,Right_id,Reject_right_id,Username,Checkid,Updatename)"
                + " VALUES (?, ?, ?, ?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(sql, rejectEntity.getId(),rejectEntity.getCreatetime(),rejectEntity.getDescriptor(), rejectEntity.getUpdatetime(),rejectEntity.getRight_id(),rejectEntity.getReject_right_id(),rejectEntity.getCreatename(),rejectEntity.getCheckid(),
					rejectEntity.getUpdatename());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}   	
    	
    };
}
